package com.kingoe.shardingdj.controller;

import com.alibaba.fastjson.JSONObject;
import com.kingoe.shardingdj.entity.TbCommodityInfo;
import com.kingoe.shardingdj.service.TbCommodityInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author jinwu
 */
@Slf4j
@RestController
@RequestMapping("/api/v1/commodity")
public class CommodityController {

    @Autowired
    private TbCommodityInfoService commodityInfoService;

    /**
     * 获取所有商品列表
     *
     * @return List<TbCommodityInfo> 商品列表
     * @author java技术爱好者
     */
    @GetMapping("/list")
    public List<TbCommodityInfo> getCommodityInfoList() throws Exception {
        return commodityInfoService.getCommodityInfoList();
    }

    /**
     * 添加商品
     *
     * @param commodityName  商品名称
     * @param commodityPrice 商品价格
     * @param description    商品价格
     * @param number         商品数量
     * @return boolean 是否添加成功
     */
    @PostMapping("/insert")
    public boolean insertCommodityInfo(@RequestParam(name = "name") String commodityName,
                                       @RequestParam(name = "price") String commodityPrice,
                                       @RequestParam(name = "desc") String description,
                                       @RequestParam(name = "num") Integer number) throws Exception {
        return commodityInfoService.insertCommodityInfo(commodityName, commodityPrice, description, number);
    }


}
